import {createRouter,createWebHistory} from "vue-router";
import {getJwt} from "@/api/jwt.js";
const routes=[{
    name:"main",
    path:"/main",
    component:()=>import("@/components/view/main.vue"),
    children:[{
        name:"member",
        path:"/main/member",
        component:()=>import("@/components/view/member.vue")
    },{
        name: "coach",
        path: "/main/coach",
        component: () => import("@/components/view/coach.vue")
    },{
        name: "classroom",
        path: "/main/classroom",
        component: () => import("@/components/view/classroom.vue")
    },{
        name:"course",
        path:"/main/course",
        component:()=>import("@/components/view/course.vue")
    },{
        name:"reservation",
        path:"/main/reservation",
        component:()=>import("@/components/view/reservation.vue")
    },{
        name:"charts",
        path:"/main/charts",
        component:()=>import("@/components/view/charts.vue")
    },{
        name:"admin",
        path:"/main/admin",
        component:()=>import("@/components/view/admin.vue")
    },{
        name:"reservationControl",
        path:"/main/reservationControl",
        component:()=>import("@/components/view/reservationControl.vue")
    },{
        name:"calendar",
        path:"/main/calendar",
        component:()=>import("@/components/view/calendar.vue")
    }
    ],

},{
    name:"login",
    path:"/login",
    component:()=>import("@/components/view/login.vue")
},{
    name:"index",
    path:"/",
    redirect:"/main/member"
}
];
const router = createRouter({
    history: createWebHistory(),
    routes
});

//配置路由守卫
router.beforeEach((to,from,next)=>{
    let jwt = getJwt()
    if (jwt){
        if (to.name === "login"){
            next("/main");
        }else {
            next();
        }
    }else {
        if (to.name!== "login"){
            next("/login");
        }
        else next();
    }
})
export default router;